package Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class paixushenfenzheng {
    public static void main(String[] args) {
        Scanner sc=  new Scanner(System.in);
        int n = sc.nextInt();
    String peo[] = new String[n];//存身份证号码
        String birth[] = new String[n];//存生日
        for(int i=0;i<n;i++){
            peo[i]=sc.next();
        }
        while(true){
            String str = sc.next();
            if(str.equals("sort1")){
                for(int i=0;i<n;i++){
                 birth[i]=peo[i].substring(6,10)+"-"+peo[i].substring(10,12)+"-"+peo[i].substring(12,14);
                }
                Arrays.sort(birth);
                for(int i=0;i<n;i++){
                    System.out.println(birth[i]);
                }
            }else if(str.equals("sort2")){
                for(int i=0;i<n;i++){
                    birth[i]=peo[i].substring(6,10)+peo[i].substring(10,12)+peo[i].substring(12,14);
                }
                Arrays.sort(birth);
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        if(peo[j].contains(birth[i])){
                            System.out.println(peo[j]);
                        }
                    }
                }
            }


        }
    }
}
